package eco2automotive.gui;
import eco2automotive.gui.components.Panel_KMHRPM;
import eco2automotive.elmFilter.Filter;
import eco2automotive.util.MathLib;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

import javax.swing.*;
import java.awt.Dimension;

/**  
  * Show a modal dialog: KMH vs R.P.M.
 * Contains a PANEL_KMHRPM panel 
 * @see eco2automotive.gui.components.Panel_KMHRPM
  * @version 1.0
  * @autor Fco. Javier Sánchez Esparza
  *
  */ 

public class Layout_KMHRPM extends JDialog{
              
        eco2automotive parent;        
        Panel_KMHRPM PANEL_KMHRPM;        

        public Layout_KMHRPM(JFrame parent){
        super(parent);        
        setTitle("Monitoring...");     
        addWindowListener( new WindowAdapter() {
 public void windowClosing( WindowEvent e ) {
 close();
 }
 });
            
        PANEL_KMHRPM=new Panel_KMHRPM();        
        this.add(PANEL_KMHRPM);                 
        this.parent=(eco2automotive)parent;              
        setSize(new Dimension(300, 300));        
        setResizable(false);
        pack();               
    }    
    
 
 private void close() {     
 dispose();
 }
 
   /** 
     * Filter, Calculate & Update the information in the panel PANEL_KMHRPM
  *  @see eco2automotive.util.MathLib
    * @see eco2automotive.elmFilter.Filter
     * @see eco2automotive.gui.components.Panel_KMHRPM    
     * @param data String to filter
    */
public void updateData(String data){
    String kmh=new Filter().getDigest(data,"410D[0-9-A-Z]{4}");    
    String rpm=new Filter().getDigest(data,"410C[0-9-A-Z]{4}");    
    
    if(!rpm.isEmpty()){
    rpm=rpm.substring(4);
    PANEL_KMHRPM.updateKMH(new MathLib().calculatorXF(rpm, "AABB", "((A*256)+B)/4"));        
    }
    if(!kmh.isEmpty()){
    kmh=kmh.substring(4);    
    PANEL_KMHRPM.updateRPM(new MathLib().calculatorXF(kmh, "AA", "A"));
    }
}

   
}
